<template>
  <div class="announcement-detail-page">
    <div class="detail-container">
      <!-- 返回按钮 -->
      <div class="back-link">
        <el-button link @click="router.back()">
          <el-icon><ArrowLeft /></el-icon>
          返回列表
        </el-button>
      </div>

      <!-- 公告内容 -->
      <div class="announcement-content">
        <div class="content-header">
          <el-tag 
            :type="announcement.type" 
            size="large" 
            effect="light"
            class="header-tag"
          >
            {{ announcement.tagText }}
          </el-tag>
          <h1>{{ announcement.title }}</h1>
          <div class="meta-info">
            <span class="time">
              <el-icon><Calendar /></el-icon>
              发布时间：{{ announcement.time }}
            </span>
            <span class="views">
              <el-icon><View /></el-icon>
              {{ announcement.views }} 次浏览
            </span>
            <span class="department">
              <el-icon><OfficeBuilding /></el-icon>
              发布部门：{{ announcement.department }}
            </span>
          </div>
        </div>

        <div class="content-body">
          <div class="rich-content" v-html="announcement.content"></div>
          
          <!-- 附件列表 -->
          <div class="attachments" v-if="announcement.attachments?.length">
            <h3>附件列表</h3>
            <div class="attachment-list">
              <div v-for="file in announcement.attachments" 
                   :key="file.id" 
                   class="attachment-item">
                <el-icon><Document /></el-icon>
                <span class="file-name">{{ file.name }}</span>
                <span class="file-size">{{ file.size }}</span>
                <el-button type="primary" link @click="downloadFile(file)">
                  下载
                </el-button>
              </div>
            </div>
          </div>
        </div>

        <!-- 评论区 -->
        <div class="comments-section">
          <h3>评论区 ({{ announcement.comments?.length || 0 }})</h3>
          
          <!-- 评论输入框 -->
          <div class="comment-input">
            <el-input
              v-model="newComment"
              type="textarea"
              :rows="3"
              placeholder="写下你的评论..."
            />
            <el-button type="primary" @click="submitComment">
              发表评论
            </el-button>
          </div>

          <!-- 评论列表 -->
          <div class="comments-list">
            <div v-for="comment in announcement.comments" 
                 :key="comment.id" 
                 class="comment-item">
              <div class="comment-avatar">
                <el-avatar :src="comment.avatar" />
              </div>
              <div class="comment-content">
                <div class="comment-header">
                  <span class="username">{{ comment.username }}</span>
                  <span class="time">{{ comment.time }}</span>
                </div>
                <div class="comment-text">{{ comment.content }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  ArrowLeft, 
  Calendar, 
  View, 
  OfficeBuilding, 
  Document 
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const newComment = ref('')

// 示例数据
const announcement = ref({
  id: 1,
  title: '关于组织海滩清洁志愿活动的通知',
  type: 'success',
  tagText: '活动',
  time: '2024-03-15 14:30',
  views: 156,
  department: '志愿者管理部',
  content: `
    <p>亲爱的志愿者们：</p>
    <p>为了保护海洋环境，提高公众环保意识，我们将于2024年3月20日组织海滩清洁志愿活动。</p>
    <h3>活动详情：</h3>
    <ul>
      <li>时间：2024年3月20日 09:00-12:00</li>
      <li>地点：阳光海滩（具体集合地点将通过短信通知）</li>
      <li>活动内容：海滩垃圾清理、垃圾分类、环保知识宣传</li>
      <li>所需物品：防晒用品、帽子、水壶（组织方将提供手套、垃圾袋等工具）</li>
    </ul>
    <p>请有意参与的志愿者于3月18日前完成报名。</p>
    <p>注意事项：</p>
    <ol>
      <li>请穿着轻便服装和运动鞋</li>
      <li>请带够饮用水</li>
      <li>活动将计入志愿服务时长</li>
    </ol>
  `,
  attachments: [
    {
      id: 1,
      name: '活动详细说明.pdf',
      size: '2.5MB'
    },
    {
      id: 2,
      name: '免责声明.docx',
      size: '521KB'
    }
  ],
  comments: [
    {
      id: 1,
      username: '海洋之心',
      avatar: 'https://avatars.githubusercontent.com/u/1?v=4',
      content: '这次活动很有意义，已经报名参加了！',
      time: '2024-03-15 15:20'
    },
    {
      id: 2,
      username: '蔚蓝守护者',
      avatar: 'https://avatars.githubusercontent.com/u/2?v=4',
      content: '请问可以带家人一起参加吗？',
      time: '2024-03-15 16:05'
    }
  ]
})

// 提交评论
const submitComment = () => {
  if (!newComment.value.trim()) {
    ElMessage.warning('请输入评论内容')
    return
  }
  
  // 这里应该调用API提交评论
  announcement.value.comments.unshift({
    id: Date.now(),
    username: '当前用户',
    avatar: 'https://avatars.githubusercontent.com/u/3?v=4',
    content: newComment.value,
    time: new Date().toLocaleString()
  })
  
  newComment.value = ''
  ElMessage.success('评论发表成功')
}

// 下载附件
const downloadFile = (file) => {
  ElMessage.success(`开始下载：${file.name}`)
  // 这里应该调用实际的下载API
}

onMounted(() => {
  // 这里应该根据路由参数获取公告详情
  console.log('Announcement ID:', route.params.id)
})
</script>

<style scoped lang="scss">
.announcement-detail-page {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 40px 0;

  .detail-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 0 20px;

    .back-link {
      margin-bottom: 20px;
    }

    .announcement-content {
      background: white;
      border-radius: 12px;
      padding: 40px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);

      .content-header {
        margin-bottom: 40px;
        text-align: center;

        .header-tag {
          margin-bottom: 20px;
        }

        h1 {
          font-size: 28px;
          color: #1f2937;
          margin-bottom: 20px;
          line-height: 1.4;
        }

        .meta-info {
          display: flex;
          justify-content: center;
          gap: 30px;
          color: #64748b;
          font-size: 14px;

          span {
            display: flex;
            align-items: center;
            gap: 6px;
          }
        }
      }

      .content-body {
        .rich-content {
          color: #1f2937;
          line-height: 1.8;
          font-size: 16px;

          h3 {
            font-size: 20px;
            margin: 30px 0 15px;
            color: #1f2937;
          }

          p {
            margin-bottom: 16px;
          }

          ul, ol {
            margin-bottom: 16px;
            padding-left: 20px;

            li {
              margin-bottom: 8px;
            }
          }
        }

        .attachments {
          margin-top: 40px;
          padding-top: 30px;
          border-top: 1px solid #e5e7eb;

          h3 {
            font-size: 18px;
            color: #1f2937;
            margin-bottom: 20px;
          }

          .attachment-list {
            display: flex;
            flex-direction: column;
            gap: 12px;

            .attachment-item {
              display: flex;
              align-items: center;
              gap: 12px;
              padding: 12px;
              background: #f8fafc;
              border-radius: 8px;

              .file-name {
                flex: 1;
                color: #1f2937;
              }

              .file-size {
                color: #64748b;
                font-size: 14px;
              }
            }
          }
        }
      }

      .comments-section {
        margin-top: 40px;
        padding-top: 30px;
        border-top: 1px solid #e5e7eb;

        h3 {
          font-size: 18px;
          color: #1f2937;
          margin-bottom: 20px;
        }

        .comment-input {
          margin-bottom: 30px;

          .el-button {
            margin-top: 16px;
          }
        }

        .comments-list {
          display: flex;
          flex-direction: column;
          gap: 20px;

          .comment-item {
            display: flex;
            gap: 16px;

            .comment-avatar {
              flex-shrink: 0;
            }

            .comment-content {
              flex: 1;

              .comment-header {
                margin-bottom: 8px;

                .username {
                  font-weight: 500;
                  color: #1f2937;
                  margin-right: 12px;
                }

                .time {
                  color: #64748b;
                  font-size: 14px;
                }
              }

              .comment-text {
                color: #374151;
                line-height: 1.6;
              }
            }
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .announcement-detail-page {
    padding: 20px 0;

    .detail-container {
      .announcement-content {
        padding: 20px;

        .content-header {
          .meta-info {
            flex-direction: column;
            align-items: center;
            gap: 10px;
          }
        }
      }
    }
  }
}
</style> 